<template>
  <el-dialog
    :title="!isUpdate? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="工号" prop="name">
        <el-input v-model="dataForm.tid" placeholder="工号"></el-input>
      </el-form-item>
    <el-form-item label="姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="联系方式" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="联系方式"></el-input>
    </el-form-item>
<!--    <el-form-item label="" prop="deptId">-->
<!--      <el-input v-model="dataForm.deptId" placeholder=""></el-input>-->
<!--    </el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {teacherApi} from "@/api/teacher";

  export default {
    data () {
      return {
        visible: false,
        isUpdate:false,
        dataForm: {
          tid: null,
          name: '',
          mobile: '',
          deptId: ''
        },
        dataRule: {
          name: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '联系方式不能为空', trigger: 'blur' }
          ],
          deptId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {

        if(id){
          this.isUpdate = true
        }
        this.visible = true
        this.$nextTick(() => {
          // this.$refs['dataForm'].resetFields()
          this.dataForm = this.$options.data().dataForm
          if (this.isUpdate) {
            teacherApi.getTeacherInfo(id).then(({teacher}) => {
              this.dataForm = teacher
            })
          }

        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            teacherApi.saveOrUpdate(this.dataForm,this.id?"update":"save")
           .then(({data}) => {
              // if (data && data.code === 20000) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              // } else {
              //   this.$message.error(data.msg)
              // }
            })
          }
        })
      }
    }
  }
</script>
